import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

class ScrollViewListVC extends StatelessWidget{

  @override
  Widget build(BuildContext context) {

    final titles = ["ListView","GridView","CostomeScrollView"];

    return Scaffold(
      appBar: AppBar(title: Text("可滚动组件"),),
      body: ListView.separated(itemBuilder: (context,i){
        return ListTile(title: Text(titles[i]),onTap: ()=> _cellDidselectForRow(context,i),);
      }, separatorBuilder: (context,i){
        return Divider();
      }, itemCount: titles.length),
    );
  }

  /*CELL 点击*/
  void _cellDidselectForRow(context,idx){
    switch(idx){
      case 0:{
        Navigator.pushNamed(context, "ListViewVC");
        break;
      }
      case 1:{

        break;
      }
      case 2:{

        break;
      }
      default:
        break;
    }
  }
}

class ListViewVC extends StatefulWidget{


  var dataSource = ["2222"];

  @override
  State<ListViewVC> createState() {
    // TODO: implement createState
    return ListViewVCState();
  }
}

class ListViewVCState extends State<ListViewVC>{

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text("ListView"),),
      body: ListView.separated(itemBuilder: (context,i){
        if(widget.dataSource.length == 0 || widget.dataSource.length - 1 == i){
          debugPrint("======== i:$i length:"+widget.dataSource.length.toString());
          _loadDataAndRefreshDataSource();
          return Text("Loading....");
        }else if (widget.dataSource.length >= 100){
          return Text("- the end -");
        }else{
          return Text(widget.dataSource[i]);
        }
      }, separatorBuilder: (context,i){
        return Divider();
      }, itemCount: widget.dataSource.length),
    );
  }

  void _loadDataAndRefreshDataSource(){
    if(widget.dataSource.length < 100){
      setState(() {
        widget.dataSource.addAll(generateWordPairs().take(20).map((e) => e.asPascalCase).toList());
      });
    }
  }
}